<?


	Class zMail {
  		var 
  			$to,
    		$from,
    		$subject,
		    $text,
		    $file,
		    $type,
		    $name;

		function Mail($to, $from, $subject) {

    		$this->to = $to;
    		$this->from = $from;
    		$this->subject = $subject;

  		}

  		function addMessage($text) {

    		$this->text = $text;
  		}

	  	function addAttachment($file, $name, $type) {

    		$this->file[] = $file;
    		$this->name[] = $name;
    		$this->type[] = $type;
  		}

  		function send() {

    		$content = fread(fopen($this->file[0],"r"),filesize($this->file[0]));
    		$content = chunk_split(base64_encode($content));
    		$uid = strtoupper(md5(uniqid(time())));

    
    
    		$header = "From: ".$this->from."\nReply-To: ".$this->from."\n";
    		$header .= "MIME-Version: 1.0\n";
    		$header .= "Content-Type: multipart/mixed; boundary=$uid\n";
    
    		$header .= "--$uid\n";
    		$header .= "Content-Type: text/plain\n";
    		$header .= "Content-Transfer-Encoding: 8bit\n\n";
    		$header .= $this->text[0]."\n";
    		$header .= "--$uid\n";
    
    		$header .= "Content-Type: ".$this->type[0]."; name=\"".$this->name[0]."\"\n";
    		$header .= "Content-Transfer-Encoding: base64\n";
    		$header .= "Content-Disposition: attachment; filename=\"".$this->name[0]."\"\n\n";
    		$header .= "$content\n";
    		$header .= "--$uid--";
    
    		#Header("Content-type: text/plain");
    #echo $header;
    
    		mail($to, $subject, "", $header);
    		return true;


  		}
	}

function sendMail($to, $subject, $text, $from, $file, $name, $type) {
  
  $content = fread(fopen($file,"r"),filesize($file));
  $content = chunk_split(base64_encode($content));
  $uid = strtoupper(md5(uniqid(time())));
#  $name = basename($file);


  $header = "From: $from\nReply-To: $from\n";
  $header .= "MIME-Version: 1.0\n";
  $header .= "Content-Type: multipart/mixed; boundary=$uid\n";

  $header .= "--$uid\n";
  $header .= "Content-Type: text/plain\n";
  $header .= "Content-Transfer-Encoding: 8bit\n\n";
  $header .= "$text\n";
  $header .= "--$uid\n";

  $header .= "Content-Type: $type; name=\"$name\"\n";
  $header .= "Content-Transfer-Encoding: base64\n";
  $header .= "Content-Disposition: attachment; filename=\"$name\"\n\n";
  $header .= "$content\n";
  $header .= "--$uid--";

  mail($to, $subject, "", $header);
  return true;

}

Class Mail {
  var 
    $to,
    $subject,
    $body,
    $from,
    $filename,
    $file,
    $filetype,
    $header;

  function eMail($to,$from,$subject,$body) {

    $this->to = $to;
    $this->from = $from;
    $this->subject = $subject;
    $this->body = $body;
  }

  function addAttachment($name,$file,$type) {

    $this->filename[] = $name;
    $this->file[] = $file;
    $this->filetype[] = $type;
  }

  function send() {

    /* DEBUG

    echo "<hr>eMail report:<p>";
    echo "To: ",$this->to,"<br>";
    echo "From: ",$this->from,"<br>";
    echo "Subject: ",$this->subject,"<p>";

    if (count($this->filename)>0) {
      echo "With attachments.";
    } else {
      echo "With no attachments.";
    }

    */


    if (count($this->filename)>0) {

      $uid = strtoupper(md5(uniqid(time())));
      $header = "From: ".$this->from."\nReply-To: ".$this->from."\n";
      $header .= "MIME-Version: 1.0\n";
      $header .= "Content-Type: multipart/mixed; boundary=$uid\n";
      $header .= "--$uid\n";
      $header .= "Content-Type: text/plain\n";
      $header .= "Content-Transfer-Encoding: 8bit\n\n";
      $header .= $this->body."\n";

    
      foreach($this->filename as $k=>$v) {
	
	$content = fread(fopen($this->file[$k],"r"),filesize($this->file[$k]));
	$content = chunk_split(base64_encode($content));

	$header .="--$uid\n";
	$header .= "Content-Type: ".$this->filetype[$k]."; name=\"".$this->filename[$k]."\"\n";
	$header .= "Content-Transfer-Encoding: base64\n";
	$header .= "Content-Disposition: attachment; filename=\"".$this->filename[$k]."\"\n\n";
	$header .= "$content\n";

      }

      $header .= "--$uid--\n";
      mail($this->to, $this->subject, $this->body, $header);

    } else {
      mail($this->to, $this->subject, $this->body,"From: ".$this->from);
    }
    
    return true;
  }
}




?>